Originally Posted by
smokeyangel
I find it hard to believe that the first one would run fine if you tried to access the elements.
Try printing the elements: surely this crashes?
Actually yes, it does. Sorry, that was a bad example. The example I should have used was in fact a struct:
Code:
struct test{
char a[5];
char b[5];
} test;
memcpy(&test, &((struct test){0,1,2,3,4,5,6,7,8,9}), sizeof(struct test));
That was giving me a warning in GCC even though the code compiled and ran. I realized, however, that I made a mistake and should have done this instead:
Code:
memcpy(&test, &((struct test){{0,1,2,3,4},{5,6,7,8,9}}), sizeof(struct test));
Originally Posted by
Click_here
Is this the sort of thing that you are after?
Code:
char arr1[5] = {0,1,2,3,4};
char arr2[5] = {5,6,7,8,9};
void *array[] = {arr1, arr2};
int i,j;
for (i = 0; i < 2; i++)
{
char (*s_array)[5] = array[i];
printf("\n");
for (j = 0; j < 5; j++)
{
printf("%d ", (*s_array)[j]);
}
}
I was hoping to avoid having to first initialize each "secondary" array individually, then initialize the pointer array with the secondary arrays, if that makes sense.
Anyway, I found this:
Using the GNU Compiler Collection (GCC)
Apparently the (char[]) cast is valid C99. I compiled my code in both GCC and Clang, and got no warnings.
I'm using GCC 4.7.2 and Apple's Clang/LLVM 3.1.